<?php

namespace app\admin\controller;

use app\admin\model\Arctile;
use app\admin\model\Content;
use app\admin\model\Tag;
use app\admin\model\Tag_correlation;
use think\Controller;
use think\Db;
use think\Request;
use app\admin\controller\Base;
use app\admin\model\Category;



class Arctiles extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $rel = Db::name('arctile')->paginate(3,false,[
            'type' => 'Bootstrap',
            'var_page' => 'page',
            'path'=>'javascript:AjaxPage([PAGE]);',
            //使用函数助手传入参数
            'query' => request()->param()
        ]);
        return view('arctile/index', ['data'=>$rel]);
    }


    public function ajaxListAction(){

        $page = request()->param('apage');

        if (!empty($page)) {
            $rel = model('arctile')->paginate(3,false,[
                'type'=> 'Bootstrap',
                'var_page' => 'page',
                'page' => $page,
                'path'=>'javascript:AjaxPage([PAGE]);'

            ]);
            $page = $rel->render();
        }
        return json(['data'=>$rel,'page'=>$page]);

    }

    public function ajaxListSearch()
    {
        $name = input('post.')['title'];
        $data = Arctile::where('title', 'like', $name.'%')->paginate(3,false,[
            'type'     => 'Bootstrap',
            'var_page' => 'page',
            'path'=>'javascript:AjaxPage([PAGE]);',
            //使用函数助手传入参数
            'query' => request()->param()
        ]);

        return json(['data'=>$data]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $info = Category::all();
        $arr =[];
        foreach ($info as $val){
            $arr[] = $val->getData();
        }
        $data = getTree($arr);
        return view('arctile/add',['data'=>$data]);
    }



    public function add(Request $request)
    {

        $info = input('post.');
        $arr['author'] = $info['author'];
        $arr['title'] = $info['title'];
        $arr['desc'] = $info['desc'];
        $arr['status'] = $info['status'];
        $arr['cateid'] = $info['cateid'];
        $file = $request->file('thumb');
        if ($file){
            $res = $file->move(ROOT_PATH  . 'public/static/upload/arc');
            if($res){

            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
               $arr['thumb'] = $res->getSaveName();

            }else{
            // 上传失败获取错误信息
               return  $file->getError();
            }
        }
        $arr['release_time'] = time();
        $arctile = new Arctile();
        $result = $arctile->save($arr);

        if ($result > 0){
            $con['id'] = $arctile->id;
            $con['content'] = $info['content'];
            Content::create($con);
            $this->success('添加成功', url('admin/index/index'));
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {

        $arctile = Arctile::get($id);
        if(!empty($arctile)){
            $arctiles = $arctile->getData();
            $con = Content::get($id)->getData()['content'];
            $cates = Category::all();
            $arr = [];
            foreach ($cates as $val){
                $arr[] = $val->getData();
            }
            $cate = getTree($arr);
            return view('arctile/edit', ['cate'=>$cate,'arc'=>$arctiles, 'con'=>$con]);
        }else{
            $this->redirect(url('admin/index/errorAdmin'));
            return;
        }


    }

    public function editArc(Request $request)
    {
        $info = input('post.');

        $arr['author'] = $info['author'];
        $arr['title'] = $info['title'];
        $arr['desc'] = $info['desc'];
        $arr['status'] = $info['status'];
        $arr['cateid'] = $info['cateid'];
        $arr['release_time'] = time();
        $file = $request->file('thumb');
        if ($file){
            $res = $file->move(ROOT_PATH  . 'public/static/upload/arc');
            if($res){
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                $arr['thumb'] = $res->getSaveName();

            }else{
                // 上传失败获取错误信息
                return  $file->getError();
            }
        }
        $arctile = new Arctile();
        $res = $arctile->save($arr, ['id'=>$info['id']]);

        if($res>0){
            $con = new Content();
            $result = $con->save(['content'=>$info['content']], ['id'=>$info['id']]);
            $this->success('修改成功', url('admin/index/index'));
        }else{
            $this->error('修改失败', '/arctile/' . $info['id']);
        }

    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $Arctile = Arctile::get($id);
        $result = $Arctile->delete();
        Tag_correlation::where('arcid', '=', $id)->delete();
        $data = [];
        if ($result>0){
            Content::get($id)->where('id','=', $id)->delete();
            $data['status'] = true;
            $data['message'] = '删除成功';
        }else{
            $data['status'] = false;
            $data['message'] = '删除失败';
        }
        return json($data);
    }

    public function assginTag()
    {
        $data = [];
        $tag = [];
        $arc = Arctile::all();
        if ($arc){
            foreach ($arc as $val){
                $id = $val->getData()['id'];
                $tagid = Tag_correlation::allTagid($id);
                $data[$id] = $val->getData();
                $data[$id]['tag'] = Tag::allTag($tagid);
            }
            $tag = Db::name('tag')->select();

        }
        return view('arctile/assignTag', ['data' => $data, 'tag' => $tag]);
    }

    public function getTag()
    {
        $info = input('post.');
        Tag_correlation::where('arcid', '=', $info['id'])->delete();
        $result = 1;
        if (count($info['tag'] ) > 0){
            $tag_corr = new Tag_correlation();
            $result = $tag_corr->add($info['id'],$info['tag']);
        }
        $data = [];
        //判断
        if ($result > 0){
            $data['status'] = true;
            $data['message'] = '添加成功';
        }else{
            $data['status'] = false;
            $data['message'] = '添加失败';
        }
        return json($data);
    }
}
